/***
 * Author: Andrey Zhmaylik
 * 		   Erez Bar
 * What:   Main menu application screen. 
 */


package house.keepper;

import java.io.IOException;
import java.io.OptionalDataException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainScreen extends Activity {
	private Button m_livePics, m_playPic, m_settings;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
        m_livePics = (Button)findViewById(R.id.Live_viewBtn);
        m_playPic = (Button)findViewById(R.id.PlayPicBtn);
        m_settings = (Button)findViewById(R.id.SettingsBtn);
        
        //Event listeners for the buttons
        m_livePics.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	PlayLivePicsFlow();
            }
                   
        });
        m_playPic.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	playSavedMedia();
            }
                   
        });
        m_settings.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	ChangeSettings();
            }
                   
        });
        
    }
    
	protected void PlayLivePicsFlow() {
		Intent i = new Intent(this, Live.class);
        startActivity(i);
		
	}

	protected void ChangeSettings() {
		Intent i = new Intent(this, Settings.class);
        startActivity(i);
	}

	protected void playSavedMedia() {	
		try {
			Connection.s_out.writeInt(ServerCommands.GetPictures.value());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			if(Connection.s_in.readInt() == ServerCommands.FileListOk.value())
			{
				Intent i = null;
				i = new Intent(this, FileBrowser.class);			
				startActivity(i);
			}
			else
			{
				TextView tv = new TextView(this);
	    		tv.setText("No files where found, press Back");
	    		tv.setTextSize(20);
	    		android.app.Dialog alertWindow = new android.app.Dialog(this);
	    		alertWindow.setTitle("Error");
	    		alertWindow.setContentView(tv);
	    		alertWindow.show();
			}

		} catch (OptionalDataException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
